package com.redstar.HappyRefresh.config;


import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Data
@Component
@ConfigurationProperties(prefix = "qiniu.kodo")
public class QiNiuConfig {

    private String accessKey; // 账号
    private String secretKey; // 密钥
    private String bucketName; // 七牛云的存储空间名称

    // CNAME用于指向用户的存储空间，七牛云的自定义域名，用于访问存储在七牛云上的文件
    public static final String CNAME = "http://sv2k7ofgr.hd-bkt.clouddn.com";
    // 用于视频内容安全审核的API的URL
    public static final String VIDEO_URL = "http://ai.qiniuapi.com/v3/video/censor";
    // 用于图像内容安全审核的API的URL
    public static final String IMAGE_URL = "http://ai.qiniuapi.com/v3/image/censor";
    // 用于定义视频处理操作
    public static final String fops = "avthumb/mp4";

    public Auth buildAuth() { // 构建一个七牛云的Auth对象，该对象用于后续的认证
        String accessKey = this.getAccessKey();
        String secretKey = this.getSecretKey();
        return Auth.create(accessKey, secretKey);
    }


    public String uploadToken() { // 生成上传Token，允许上传视频或图像文件
        Auth auth = buildAuth();
        return auth.uploadToken(bucketName, null, 300,
                new StringMap().put("mimeLimit", "video/*;image/*"));
    }

    public String videoUploadToken() { // 生成专门用于视频的上传Token，附带了漏斗操作
        Auth auth = buildAuth();
        return auth.uploadToken(bucketName, null, 300,
                new StringMap().put("mimeLimit", "video/*").putNotEmpty("persistentOps", fops));
    }

    public String imageUploadToken() { // 生成专门用于图像的上传Token
        Auth auth = buildAuth();
        return auth.uploadToken(bucketName, null, 300, new StringMap().put("mimeLimit", "image/*"));
    }

    public String getToken(String url, String method, String body, String contentType) { // 生成签名Token，用于发送HTTP请求
        Auth auth = buildAuth();
        return "Qiniu " + auth.signQiniuAuthorization(url, method, body == null ? null : body.getBytes(), contentType);
    }

}